package com.cbs.android.extension.recyclerviewadapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by broche on 8/10/16.
 */
public abstract class RecyclerViewAdapter extends RecyclerView.Adapter {
    protected static final int TYPE_HEADER = -1;
    protected static final int TYPE_EMPTY = -2;

    private SingleViewHolder headerViewHolder = null;
    private SingleViewHolder emptyViewHolder = null;

    @Override
    public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        if (viewType == TYPE_HEADER) {
            viewHolder = headerViewHolder;
            if (viewHolder == null) {
                viewHolder = new SingleViewHolder(new View(parent.getContext()));
            }
        } else if (viewType == TYPE_EMPTY) {
            viewHolder = emptyViewHolder;
            if (viewHolder == null) {
                viewHolder = new SingleViewHolder(new View(parent.getContext()));
            }
        } else {
            viewHolder = onCreateContentViewHolder(parent, viewType);
        }
        return viewHolder;
    }

    public void setHeaderView(View headerView) {
        if (headerView != null) {
            headerViewHolder = new SingleViewHolder(headerView);
        } else {
            headerViewHolder = null;
        }
    }

    public View getHeaderView() {
        if (headerViewHolder != null) {
            return headerViewHolder.itemView;
        } else {
            return null;
        }
    }

    public View getEmptyView() {
        if (emptyViewHolder != null) {
            return emptyViewHolder.itemView;
        } else {
            return null;
        }
    }

    public void setEmptyView(View emptyView) {
        if (emptyView != null) {
            emptyViewHolder = new SingleViewHolder(emptyView);
        } else {
            emptyViewHolder = null;
        }
    }

    protected abstract RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent, int viewType);

    @Override
    public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemType = getItemViewType(position);
        if (itemType == TYPE_HEADER) {
            // do nothing
        } else if (itemType == TYPE_EMPTY) {
            // do nothing
        } else {
            onBindContentViewHolder(holder, position - 1);
        }
    }

    protected abstract void onBindContentViewHolder(RecyclerView.ViewHolder holder, int position);

    @Override
    public final int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return TYPE_HEADER;
        } else if (isEmptyView(position)) {
            return TYPE_EMPTY;
        } else {
            return getContentViewType(position - 1);
        }
    }

    protected int getContentViewType(int position) {
        return 0;
    }

    @Override
    public final int getItemCount() {
        int contentCount = getContentCount() + 1; // header view
        if (getContentCount() == 0) { // empty view
            contentCount++;
        }
        return contentCount;
    }

    public abstract int getContentCount();

    public void notifyContentChanged(int position) {
        notifyItemChanged(position + 1);
    }

    public boolean isHeaderView(int position) {
        return position == 0;
    }

    public boolean isEmptyView(int position) {
        return (getContentCount() == 0) && (position == 1);
    }

    private class SingleViewHolder extends RecyclerView.ViewHolder {
        public SingleViewHolder(View itemView) {
            super(itemView);
        }
    }
}

